/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facade;

import entity.CurrencyEE;
import java.util.Collection;
import java.util.logging.Logger;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import ws.ExchangerServiceService;

/**
 *
 * @author Vojtisek
 */
@Stateless
public class CurrencyFacadeBean implements CurrencyFacadeLocal {

    @PersistenceContext
    private EntityManager em;
    private static final Logger logger = Logger.getLogger("CurrencyFacadeBean");

    public CurrencyEE find(String name) throws EJBException, NoResultException, Exception {
        try {
            return (CurrencyEE) em.createQuery("select o from CurrencyEE as o where o.name = :name").setParameter("name", name).getSingleResult();
        } catch (NoResultException ex) {
            return (CurrencyEE) em.createQuery("select o from CurrencyEE as o where o.name = :name").setParameter("name", name).getSingleResult();
        }
    }

    public Collection<CurrencyEE> findAll() throws Exception {
        return em.createQuery("select object(o) from CurrencyEE as o").getResultList();
    }

    private void init() {
//        try {
//            CurrencyEE c = new CurrencyEE("CZK");
//            em.persist(c);
//            c = new CurrencyEE("USD");
//            em.persist(c);
//            c = new CurrencyEE("EUR");
//            em.persist(c);
//        } catch (Exception e) {
//        }
    }
}